package com.tengju.data.domain.model.report;

import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.domain.shared.IdObject;
import com.tengju.data.domain.shared.tree.TreeData;
import lombok.Getter;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2020/12/28
 */
@Getter
public class AchievementBonusResult implements TreeData {

    private UserIdCode idCode;

    private UserIdCode pIdCode;

    private UserIdCode settlePIdCode;

    private LocalDateTime logTime;

    private Map<Integer, AchievementBonus> bonusMap = new HashMap<>();

    private Map<Integer, AchievementBonusCalMidInfo> bonusCalMidInfoMap = new HashMap<>();

    private LocalDateTime compareLogTime;

    private Map<Integer, AchievementBonus> compareBonusMap = new HashMap<>();

    private Map<Integer, AchievementBonusCalMidInfo> compareBonusCalMidInfoMap = new HashMap<>();

    public AchievementBonusResult(UserIdCode idCode) {
        this.idCode = idCode;
    }

    @Override
    public IdObject getId() {
        return idCode;
    }

    public void addBonusResult(LocalDateTime logTime, List<AchievementBonus> userBonusList, List<AchievementBonusCalMidInfo> userBonusCalMidInfoList) {
        this.logTime = logTime;
        if (!CollectionUtils.isEmpty(userBonusList)) {
            this.bonusMap = userBonusList.stream()
                    .collect(Collectors.toMap(k -> k.getTeamRatioType(), v -> v,(v1, v2) -> v1));
            this.pIdCode = userBonusList.get(0).getPIdCode();
            this.settlePIdCode = userBonusList.get(0).getSettlePIdCode();
        }
        if (!CollectionUtils.isEmpty(userBonusCalMidInfoList)) {
            this.bonusCalMidInfoMap = userBonusCalMidInfoList.stream()
                    .collect(Collectors.toMap(k -> k.getTeamRatioType(),v -> v,(v1,v2) -> v1));
        }
    }

    public void addCompareBonusResult(LocalDateTime logTime, List<AchievementBonus> userBonusList, List<AchievementBonusCalMidInfo> userBonusCalMidInfoList) {
        this.compareLogTime = logTime;
        if (!CollectionUtils.isEmpty(userBonusList)) {
            this.compareBonusMap = userBonusList.stream()
                    .collect(Collectors.toMap(k -> k.getTeamRatioType(), v -> v,(v1, v2) -> v1));
        }
        if (!CollectionUtils.isEmpty(userBonusCalMidInfoList)) {
            this.compareBonusCalMidInfoMap = userBonusCalMidInfoList.stream()
                    .collect(Collectors.toMap(k -> k.getTeamRatioType(),v -> v,(v1,v2) -> v1));
        }
    }

    public void combineCompare(AchievementBonusResult compareUserBonusResult) {
        if (compareUserBonusResult == null) {
            return;
        }
        this.compareLogTime = compareUserBonusResult.getLogTime();
        this.compareBonusMap = compareUserBonusResult.getBonusMap();
        this.compareBonusCalMidInfoMap = compareUserBonusResult.getBonusCalMidInfoMap();
    }
}
